package com.em.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.em.dao.DeviceMapper;
import com.em.dao.PropertyMapper;
import com.em.entity.Device;
import com.em.entity.Property;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceService {
    @Autowired
    private DeviceMapper mapper;

    public List<Device> select(String name) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(name)) {
            queryWrapper.like("name", name);
        }
        return mapper.selectList(queryWrapper);
    }

    public int create(Device entity) {
        long currentTime = System.currentTimeMillis();
        entity.setCreateTime(currentTime);
        entity.setUpdateTime(currentTime);
        return mapper.insert(entity);
    }

    public int update(Device entity) {
        entity.setUpdateTime(System.currentTimeMillis());
        return mapper.updateById(entity);
    }

    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

    public Device selectById(Integer id) {
        return mapper.selectById(id);
    }
}
